package com.tedu.egmvc.controller;

import com.tedu.egmvc.mapper.AddressMapper;
import com.tedu.egmvc.pojo.dto.AddressDto;
import com.tedu.egmvc.pojo.entity.Address;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class AddressController {
    @Autowired
    private AddressMapper addressMapper;

    @PostMapping("/address/add")
    public String add( AddressDto addressDto) {
        Address address = new Address();
        BeanUtils.copyProperties(addressDto, address);
        addressMapper.insertAddress(address);
        return "添加地址成功";
    }

    @DeleteMapping("/address/delete/{id}")
    public String delete(@PathVariable Integer id) {

        return addressMapper.deleteAddress(id) > 0 ? "删除地址成功" : "删除地址失败";
    }

    @PutMapping("/address/update")
    public String update(AddressDto addressDto) {
        Address address = new Address();
        BeanUtils.copyProperties(addressDto, address);
        return addressMapper.updateAddress(address) > 0 ? "修改地址成功" : "修改地址失败";
    }

    @GetMapping("/address/find")
    public List<Address> find() {
        List<Address> addressList = addressMapper.selectAddress();
        return addressList;
    }

}
